//时间选择框
if (typeof $('.datepicker').datepicker !== "undefined") {
    $('.datepicker').datepicker({
        language: 'zh-CN',
        autoclose: true,
        minView: 1,
        endDate: '0',
        todayHighlight: true,
        keyboardNavigation: true,
    });
}

//提示框
$(function () {
    $('[data-toggle="tooltip"]').tooltip({trigger: "hover"})
    $("[data-toggle='popover']").popover({trigger: "hover"});


})

//渠道checkbox
$('#tdChannels').find(':checkbox').change(function () {
    var channel_id = $(this).val();
    if (this.checked) {
        $('#tdServers div[data-channel-id="' + channel_id + '"]').show().find("input:checkbox").prop('checked', true);
    } else {
        $('#tdServers div[data-channel-id="' + channel_id + '"]').hide().find("input:checkbox").prop('checked', false);
    }
});

//渠道radio
$('#tdChannels').find(':radio').change(function () {
    var channel_id = $(this).val();
    if (this.checked) {
        $('#tdServers div').hide();
        $('#tdServers div[data-channel-id="' + channel_id + '"]').show().find("input:radio").prop('checked', false);
    }
});

//游戏radio
$('#tdGames').find(':radio').change(function () {
    var game_id = $(this).val();
    if (this.checked) {
        $('#tdChannels div').hide().find("input:radio").prop('checked', false);
        $('#tdChannels div[data-game-id="' + game_id + '"]').show().find("input:radio").prop('checked', false);
    }
});

//前端分页table
if (typeof ($("[data-toggle='table']").bootstrapTable) === "function") {
    $("[data-toggle='table']").bootstrapTable({
        pageList: [10, 20, 50, 100],
        showLoading: false,
    });
    $("[data-toggle='table']").bootstrapTable('hideLoading');
}

//获取url参数
//  http://www.runoob.com/index.php?id=1&image=awesome.jpg
//  getQueryVariable("id")
//  getQueryVariable("image")
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return (false);
}

//iframe加载完成后,绑定事件
window.onload = function () {
    if (typeof parent.iframeLoaded !== "undefined") {
        parent.iframeLoaded();
    }
}

//table排序
$('#trOrder a').click(function () {
    var new_order_column = $(this).attr('data-column');
    var old_order_column = $('input[name="order_by"]').val();
    var order_direction = $('input[name="order_direction"]').val();

    if (new_order_column != old_order_column) {
        order_direction = 'ASC';
    } else if (new_order_column == old_order_column) {
        order_direction = (order_direction == 'ASC' ? 'DESC' : 'ASC');
    }

    $('input[name="order_direction"]').val(order_direction);
    $('input[name="order_by"]').val(new_order_column);
    document.forms[0].submit();
});

//显示排序的上下箭头
var order_column = $('input[name="order_by"]').val();
var order_direction = $('input[name="order_direction"]').val();
if (order_column != '' && order_direction != '') {
    order_direction = (order_direction == 'ASC' ? 'up' : 'down');
    var html = '<div class="ibox-tools" style="float: none;"> <a class="collapse-link"> <i class="fa fa-chevron-' + order_direction + '"></i> </a> </div>';
    $('#trOrder a[data-column="' + order_column + '"]').append(html);
}

//带搜索的select下拉框
if (typeof $(".chosen-select").chosen !== "undefined") {
    $(".chosen-select").chosen();
}

//checkbox全选
function checkAll(obj) {
    $(obj).parents('.b-group').eq(0).find("input[type='checkbox']:visible").prop('checked', obj.checked).change();
}
